<template>
  <div id="globalHeader">
    <a-row :wrap="false">
      <a-col flex="200px">
        <router-link to="/">
          <div class="title-bar">
            <img class="logo" src="../assets/logo.svg" alt="logo" />
            <div class="title">happy 2025</div>
          </div>
        </router-link>
      </a-col>
      <a-col flex="auto">
        <a-menu
          v-model:selectedKeys="current"
          mode="horizontal"
          :items="items"
          @click="doMenuClick"
        />
      </a-col>
      <a-col flex="120px">
        <div v-if="username != null">
          {{ username }}
          <a-button type="primary" @click="logout">退出</a-button>
        </div>
        <div v-else class="user-login-status">
          <a-button type="primary" href="/user/login">登录</a-button>
        </div>
      </a-col>
    </a-row>
  </div>
</template>
<script lang="ts" setup>
import { computed, h, ref } from 'vue'
import { MenuProps } from 'ant-design-vue'
import { useRouter } from 'vue-router'

const router = useRouter()

const current = ref<string[]>([])

const items = ref<MenuProps['items']>(
  router.getRoutes()
    .filter((route) => !route.meta?.hidden)
    .map((route) => ({
      key: route.path,
      label: route.name,
      title: route.name,
    }))
    .concat({
      key: 'others',
      label: h('a', { href: 'https://www.codefather.cn', target: '_blank' }, '编程导航'),
      title: '编程导航',
    }),
)

const doMenuClick = ({ key }: { key: string }) => {
  router.push({
    path: key,
  })
}

import { userStore } from '@/stores/user'

const store = userStore()

const username = computed(() => store.username)

function logout() {
  store.setUsername(null)
  // 清除 cookie   JSESSIONID
  document.cookie = 'JSESSIONID=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;'
  router.push({ name: 'home' })
}
</script>

<style scoped>
#globalHeader .title-bar {
  background-color: white;
  display: flex;
  align-items: center;
}

.title {
  color: black;
  font-size: 18px;
  margin-left: 16px;
}

.logo {
  height: 48px;
}

.user-login-status {
  background-color: white;
}
</style>
